import { request } from '@/api/request'
import { useRequest } from 'ahooks'

// 其他存储方式
interface DictDataEntity {
  dictType: number
  guid: string
  code: string
  name: string
}

interface DictDataEntityExpand {
  value?: string
  remark?: string
  picUrl?: string
  picUrl2?: string
  parentGUID?: string
  childDict?: unknown
}

type DictDataComb = DictDataEntity & DictDataEntityExpand
export default function useNewDictData<U = DictDataComb>(
  dictType: number,
  mapper?: (
    value: Readonly<DictDataComb>,
    index: number,
    self: DictDataComb[]
  ) => U,
  options?: {
    refreshDeps?: React.DependencyList
    cacheKey?: string
    cacheTime?: number
  }
) {
  const { data = [] } = useRequest(
    async () => {
      const { data: res } = await request<
        DictDataComb[],
        Partial<Pick<DictDataEntity, 'dictType'>>
      >({
        url: '/account/common/Bs/api/BasicData/getDictByType.htm',
        params: { dictType },
        method: 'get'
      })
      if (mapper) {
        return res.map(mapper) as U[]
      } else {
        return res as U[]
      }
    },
    {
      ...options
    }
  )

  return [data]
}
